package cmich.cps.scheduler.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import cmich.cps.scheduler.SchedulingSystem;
import cmich.cps.scheduler.data.AssignedCourse;
import cmich.cps.scheduler.data.Schedule;

/**
 * The Class MasterScheduleController.
 * 
 * @author Jonathan Kissinger <kissi1j@cmich.edu>
 */
public class MasterScheduleController implements Controller {
	
	/** The table. */
	private final JTable table;
	
	/** The model. */
	private DefaultTableModel model;
	
	/** The system. */
	private SchedulingSystem system = SchedulingSystem.getInstance();

	/**
	 * Instantiates a new master schedule controller.
	 *
	 * @param table the table
	 */
	MasterScheduleController(JTable table) {
		this.table = table;
		String[] headers = { "Designator", "Number", "Section Number", "Professor", "Room", "Timeslot", "Enrollment", "Lab" };
		this.model = new DefaultTableModel(new Object[][] {}, headers ) {
			private static final long serialVersionUID = 1L;
			@Override
			public boolean isCellEditable(int rowIndex, int mColIndex) {
				return false;
			}
		};
		this.table.setModel(this.model);
		this.table.setAutoCreateRowSorter(true);
		this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.table.getTableHeader().setReorderingAllowed(false);
	}

	/* (non-Javadoc)
	 * @see cmich.cps.scheduler.gui.Controller#populate()
	 */
	public void populate() {
		model.setRowCount(0);
		Schedule schedule = system.getMasterSchedule();
		for(AssignedCourse c : schedule.getCourses()) {
			model.addRow(new Object[] { c.getName().split("\\s")[0], c.getName().split("\\s")[1], c.getSectionNumber(), c.getProfessor(), c.getRoom(), c.getTimeslot(), c.getEnrollment(), c.getLab()});
		}
	}

	/* (non-Javadoc)
	 * @see cmich.cps.scheduler.gui.Controller#addFunctionality()
	 */
	public void addFunctionality() {
		table.addMouseListener(new TableMenu());
		table.getParent().addMouseListener(new TableMenu());
	}

	/**
	 * @author Jonathan Kissinger <kissi1j@cmich.edu>
	 * 
	 * The Class TableMenu.
	 */
	private class TableMenu extends MouseAdapter {
		
		/* (non-Javadoc)
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				Schedule schedule = system.getMasterSchedule();
				JPopupMenu popup = new JPopupMenu();
				JMenuItem assignItem = new JMenuItem("Assign class");
				assignItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if(system.addManuallyAssignedCourse()) {
							populate();
						}
						
					}
				});
				popup.add(assignItem);
				int row = table.rowAtPoint(e.getPoint());
				if (row >= 0 && row < table.getRowCount()) {
					final int index = table.convertRowIndexToModel(row);
					AssignedCourse course = schedule.getCourses().get(index);
					JMenuItem removeItem = new JMenuItem("Unassign " + course.getName() + " (Section: " + course.getSectionNumber() + ")");
					removeItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							system.removeManuallyAssignedCourse(index);
							populate();
						}
					});
					popup.add(removeItem);
					table.setRowSelectionInterval(row, row);
				}
				JMenuItem clearItem = new JMenuItem("Clear all manually assigned courses");
				clearItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						system.clearManualCourses();
						populate();
					}
				});
				popup.add(clearItem);
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
}